package ar.com.android.alco.forms;

import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import ar.com.android.alco.entities.ControlPeso;
import ar.com.android.alco.providers.ControlPesoProvider;

public class CurvaPesoActivity extends Activity {
	private static final String TAG = "CurvaPesoActivity";
	
	private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
	private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
	private GraphicalView mChartView;
	TextView tituloActivity;
	
    @Override
    public void onStart()
    {
    	super.onStart();
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.curva_perso_chart);
        
        tituloActivity = (TextView) findViewById(R.id.txtTitulo);
        SpannableString content = new SpannableString(getString(R.string.CurvaPeso));
        content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
        tituloActivity.setText(content);
        
        try
        {
        
	        mRenderer.setAxisTitleTextSize(10);
	        mRenderer.setChartTitleTextSize(20);
	        mRenderer.setLabelsTextSize(15);
	        mRenderer.setLegendTextSize(15);
	        mRenderer.setShowGrid(true);
	        mRenderer.setMargins(new int[] {15, 15, 15, 15 });
	        mRenderer.setShowLegend(false);
	        mRenderer.setAntialiasing(true);
	        mRenderer.setZoomButtonsVisible(true);
	        mRenderer.setPointSize(4);
	        
	        XYSeriesRenderer renderer = new XYSeriesRenderer();
	        renderer.setDisplayChartValues(true);
	        renderer.setFillBelowLine(true);
	        renderer.setPointStyle(PointStyle.CIRCLE);
	        renderer.setFillPoints(true);	        
	        renderer.setColor(Color.GREEN);
	        
	        mRenderer.addSeriesRenderer(renderer);
	        
	        XYSeries series = new XYSeries(getString(R.string.ControlPeso));

    		ControlPesoProvider controlPesoProvider = new ControlPesoProvider(CurvaPesoActivity.this);
    		List<ControlPeso> lstControlPeso = controlPesoProvider.getControlesPeso();
    		
    		double xValue = 1;
    		for (ControlPeso controlPeso: lstControlPeso) {
    			series.add(xValue++, controlPeso.getPeso());
			}
	        mDataset.addSeries(series);
	        
	        mChartView = ChartFactory.getLineChartView(CurvaPesoActivity.this, mDataset, mRenderer);
	        
	        LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
	        layout.addView(mChartView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        }
        catch (Exception e) {
        	Log.e(TAG, e.getMessage());
        }
    }
}
